package com.ruoyi.mdkt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.mdkt.entity.Task;
import com.ruoyi.mdkt.entity.TaskInfo;
import com.ruoyi.mdkt.entity.vo.SignVO;
import com.ruoyi.mdkt.mapper.TaskInfoMapper;
import com.ruoyi.mdkt.mapper.TaskMapper;
import com.ruoyi.mdkt.service.TaskInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.mdkt.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gwl
 * @since 2022-10-25
 */
@Service
public class TaskInfoServiceImpl extends ServiceImpl<TaskInfoMapper, TaskInfo> implements TaskInfoService {

    @Autowired
    private TaskInfoMapper taskInfoMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private TaskInfoService taskInfoService;

    @Autowired
    private TaskService taskService;

    @Override
    public TaskInfo createSignTaskInfo(SignVO signVO, Task task, Long userId) {
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setTaskId(task.getId());
        taskInfo.setType(4);
        taskInfo.setTeacherUserId(userId);
        taskInfo.setAnswer(signVO.getKey());
        taskInfo.setQuestion(signVO.getKey());
        taskInfo.setScore(1);
        taskInfo.setCourseId(signVO.getCourseId());
        save(taskInfo);
        return taskInfoMapper.getSignTaskInfo(String.valueOf(task.getId()));
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public R saveTitle(List<TaskInfo> list) {
        for (TaskInfo taskInfo: list) {
            if (!taskInfoService.save(taskInfo)){
                throw new RuntimeException();
            }
        }
        return R.ok();
    }


}
